//
//  NSString+RegulerExpression.m
//  WSMallBuyer
//
//  Created by hedong on 2017/12/6.
//  Copyright © 2017年 hedong. All rights reserved.
//

#import "NSString+RegulerExpression.h"

NSString *emptyString(id obj) {
    if ([obj isKindOfClass:[NSNumber class]]) {
        return [obj description];
    }
    if (!obj || [obj isKindOfClass:[NSNull class]] || ![obj isKindOfClass:[NSString class]]) {
        return @"";
    }
    return obj;
}

NSString *validString(NSString *string) {
    return [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

@implementation NSString (RegulerExpression)

- (BOOL)isValid {
    if (!self || self.length == 0 || [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length == 0) {
        return NO;
    }
    return YES;
}

- (BOOL)isValidatePhone {
    NSString *phoneRegex = @"^\\d{11}$";
    NSPredicate *phonePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phonePredicate evaluateWithObject:self];
}

- (BOOL)isValidatePassword {
    NSString *passwordRegex = @"^[A-Za-z0-9]{6,15}$";
    NSPredicate *passwordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passwordRegex];
    return [passwordPredicate evaluateWithObject:self];
}

- (BOOL)isValidateNickname {
    NSString *passwordRegex = @"^[\u4E00-\u9FA5A-Za-z0-9_]{0,30}$";
    NSPredicate *passwordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passwordRegex];
    return [passwordPredicate evaluateWithObject:self];
}

@end
